/*
 *  AudioChannel.h
 *  SoundController0002
 *
 *  Created by jiro on 10/06/30.
 *  Copyright 2010 jiro music. All rights reserved.
 *
 */

#pragma once

#include <vector>
#include "AudioTrack.h"

namespace sc 
{
	template< class AudioData >
	class AudioChannel 
	{
	protected:

		std::vector< AudioTrack< AudioData > > audioTracks;

	public:

		void Initialize( int trackCount )
		{
			audioTracks.resize( trackCount );
		}

		void Record( double time, int trackId, const SoundElement< AudioData >* soundElement )
		{
			audioTracks[ trackId ].Record( time, soundElement );
		}

		void Record( int trackId, const AudioElement< AudioData >& audioElement )
		{
			audioTracks[ trackId ].Record( audioElement );
		}

		int GetTrackCount() const
		{
			return static_cast<int>( audioTracks.size() );
		}

		void GetAudioData( std::vector< AudioData >& audioData ) const
		{
			size_t maxDataCount = 0;
			int count = GetTrackCount();
			std::vector< std::vector< AudioData > > audioDataVector( count );
			for ( int i = 0; i < count; i++ )
			{
				audioTracks[ i ].GetAudioData( audioDataVector[ i ] );
				if ( audioDataVector[ i ].size() > maxDataCount )
				{
					maxDataCount = audioDataVector[ i ].size();
				}
			}

			audioData.clear();
			audioData.resize( maxDataCount, 0.0 );
			for ( int i = 0; i < count; i++ )
			{
				size_t dataCount = audioDataVector[ i ].size();
				for ( int j = 0; j < dataCount; j++ )
				{
					audioData[ j ] += audioDataVector[ i ][ j ];
				}
			}
		}

		void GetAudioData( int trackId, std::vector< AudioData >& audioData ) const
		{
			audioTracks[ trackId ].GetAudioData( audioData );
		}

	};
	
	typedef AudioChannel< float > AudioChannel32;
}
